﻿using System;
using System.Configuration;
using System.Web.Mvc;
using System.Web.Routing;
using ActivateYourGlutes.Controllers;
using ActivateYourGlutes.Data;
using ActivateYourGlutes.Helpers;
using ActivateYourGlutes.Infrastructure;
using ActivateYourGlutes.Infrastructure.Logging;
using ActivateYourGlutes.Infrastructure.MembershipServices;
using ActivateYourGlutes.Infrastructure.Unity;
using ActivateYourGlutes.Services;
using ActivateYourGlutes.Services.Implementation;
using log4net.Config;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.ServiceLocatorAdapter;
using Body=ActivateYourGlutes.Services.Model.Body;

namespace ActivateYourGlutes
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("favicon.ico");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            XmlConfigurator.Configure();
            ModelBootstrapper.Initialise();
            RegisterRoutes(RouteTable.Routes);

            IUnityContainer container = ConfigureContainer();
            ControllerBuilder.Current.SetControllerFactory(new CommonServiceLocatorControllerFactory());
            ModelBinders.Binders.Add(typeof(DateTime), new LocalisedDateTimeBinder());
            ModelBinders.Binders.Add(typeof(DateTime?), new LocalisedDateTimeBinder());
            ModelBinders.Binders.Add(typeof(Body), new BodyBinder());

            container.Resolve<ILogger>().Info("Starting up");
        }

        private static IUnityContainer ConfigureContainer()
        {
            IUnityContainer container = new UnityContainer();

            container.RegisterType<IEntityModelFactory, EntityModelFactory>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<ILocalisationProvider, LocalisationProvider>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<IAccountProvider, AccountProvider>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<IBodyProvider, BodyProvider>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<ILookupProvider, LookupProvider>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<IGoalProvider, GoalProvider>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<IErrorLogProvider, ErrorLogProvider>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<IMeasurementTypeProvider, MeasurementTypeProvider>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<AccountController>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<HomeController>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<BodyController>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<GoalController>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<ErrorLogController>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<AdministerUsersController>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<ErrorLogController>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<MeasurementTypeController>(new UnityPerWebRequestLifetimeManager())
                .RegisterType<IFormsAuthentication, FormsAuthenticationService>()
                .RegisterType<IMembershipService, AccountMembershipService>(new InjectionConstructor())
                .RegisterType<ILogger, Log4NetLogger>();

            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
            return container;
        }
    }
}